package cz.cvut.fit.mi_paa.utils;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;

/**
 * Created with IntelliJ IDEA.
 * User: milos
 * Date: 1.10.12
 * Time: 12:08
 * To change this template use File | Settings | File Templates.
 */
public class StopWatch {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    private long beginCPUTime;
    private long endCPUTime;
    private boolean running=false;

    public void start() {
        running=true;
        beginCPUTime = threadBean.getCurrentThreadCpuTime();
    }

    public void stop() {
        endCPUTime = threadBean.getCurrentThreadCpuTime();
        running=false;
    }

    public void reset() {
        beginCPUTime=0;
        endCPUTime=0;
    }

    public double getTimeMillis() {
        if(running) {
            long splitTime = threadBean.getCurrentThreadCpuTime();
            return ((double) (splitTime - beginCPUTime)* Math.pow(10, -6));
        } else {
            return ((double) (endCPUTime - beginCPUTime)* Math.pow(10, -6));
        }
    }

}
